import axios from "axios";
import { Toast } from "vant";
const http = axios.create({});

axios.defaults.timeout = 50000;
axios.defaults.baseURL = process.env.VUE_APP_BASEURL;
http.interceptors.request.use(
    config => {
        if(process.env.NODE_ENV === "production"){
            config.url=config.url.replace(/\/api/g,``);
        }
        console.log(config.url);
        return config;
    },
    error => {
        Promise.reject(error);
    }
);
// 请求的地方设置成true
http.interceptors.request.use(
    response => {
        // let token = sessionStorage.getItem("token");
        // console.log(token,response)
        // if(token){
        //   response.data.token= token
        // }
        // Toast.loading({
        //   mask: true,
        //   message: "加载中..."
        // });
        return response;
    },
    error => {
        Promise.reject(error);
    }
);

//返回参数时的拦截
http.interceptors.response.use(
    response => {
        // console.log(response)
        if (response.status == 200) {
            // 接口真正成功
            if (response.data.status == true) {
                // console.log("进来了");
                Toast.clear();
                return response;
            } else {
                Toast(response.data.msg);
                // console.log("flase",'++++++++++++++++++++++++++++++');
                return response;
            }
        } else {
            // 接口后台逻辑有bug
            Toast(response.msg);
            console.log("status!=200");
        }
    },
    error => {
        Toast(error.message);
        // console.log("服务器出错");
        // return response;
    }
);

export default http;
